/**
 * Created by Administrator on 2017/5/3.
 */
"use strict";
swan_pc
    .controller("OppPerformanceController",function($scope,$rootScope,reportList,myOrgContact,OrgCompanyInfo,tagSetByOrg,ngDialog,productNew,opportunityByEO,
                                                    opportunityByCanSee){
        $scope.width7=((document.body.clientWidth-60)/12*10-35)/7;


        //1 年度业绩变化 2 季度业绩变化 3 季度业绩分析 4 年度业绩成分分析 5 某财季业绩预测
        $scope.performanceMode = 1;
        $scope.changePerformanceMode = function(option){
            if(option==5){
                swal("此功能尚未启用，敬请期待");
                return;
            }
            $scope.performanceMode = option;
        };
        $scope.limitMode = 1;
        $scope.changeLimitMode = function(option){
            $scope.limitMode = option;
        };

        //1 个人 2 团队 3 对比
        $scope.displayMode = 1;
        $scope.changeDisplayMode = function(option){
            $scope.displayMode = option;
            if(option == 3){
                $scope.selectedItem3 = null;
                $scope.selectedItem4 = null;
            }
        };

        $scope.initLimitation = function(){
            $scope.area = false;
            $scope.address = false;
            $scope.product = false;
            $scope.tag = false;
            $scope.partner = false;



            $scope.productList = [];
            $scope.partnerList = [];
        };
        $scope.initLimitation();
        $scope.initChooseAddess = function(){
            $scope.choseAddress = {
                province:null,
                city:null,
                district:null,
                street:null
            };
        };
        $scope.initChooseAddess();

        $scope.selectedItem1 = null;
        $scope.selectedItem2 = null;
        $scope.selectedItem3 = null;
        $scope.selectedItem4 = null;
        $scope.getReportLine = function(){
            $scope.reportLine = [];
            $scope.reportLine2 = [];
            myOrgContact.get({id:$rootScope.presentAccountInfo.id},function(result){
                reportList.query({id:result.id},function(data){
                    _.forEach(data,function(item){
                        if(item.friendId!=null){
                            $scope.reportLine.push(item);
                        }
                    });

                    // console.log(data);
                });
                reportList.queryAll({id:result.id},function(data1){
                    $scope.reportLine2.push(data1);
                })
            })
        };
        $scope.getReportLine();
        $scope.yearList = [];
        $scope.seasonList = [
            {
                name:"第一季度",
                value:1
            },
            {
                name:"第二季度",
                value:2
            },
            {
                name:"第三季度",
                value:3
            },
            {
                name:"第四季度",
                value:4
            }
        ];
        $scope.generateYearList = function(){
            for(var i=2010;i<2021;i++){
                $scope.yearList.push(i);
            }
        };
        $scope.generateYearList();

        $scope.choseTwoThisContact = function(option){
            if($scope.selectedItem3 == null){
                $scope.selectedItem3 = option;
            }else {
                if($scope.selectedItem3.id != option.id){
                    $scope.selectedItem4 = option;
                }
            }
        };
        $scope.checkIfSelected = function(option){
            var flag = false;
            if($scope.selectedItem3 != null){
                if($scope.selectedItem3.id == option.id){
                    flag = true;
                }
            }
            if($scope.selectedItem4 != null){
                if($scope.selectedItem4.id == option.id){
                    flag = true;
                }
            }
            return flag;
        };


        $scope.choseThisContact = function(option){
            $scope.selectedItem1 = option;
        };

        $scope.initYearAndSeason = function(){
            $scope.year1=2016;
            $scope.year2=2017;
            $scope.year3=2016;
            $scope.season3=1;
            $scope.year4=2017;
            $scope.season4=4;
            $scope.year5=2017;
            $scope.year6=2017;
            $scope.season6=1;
        };
        $scope.initYearAndSeason();
        $scope.initGoogleChart = function(){
            switch($scope.performanceMode){
                case 1:
                    $scope.performanceChangeByYear();
                    break;
                case 2:
                    $scope.performanceChangeBySeason();
                    break;
                case 3:
                    $scope.performanceComponentByYear();
                    break;
                case 4:
                    $scope.performanceComponentBySeason();
            }
        };

        $scope.performanceComponentBySeason = function(){
            switch($scope.limitMode){
                case 1:
                    $scope.generateAreaData();
                    break;
                case 2:
                    $scope.generateAddressData();
                    break;
                case 3:
                    $scope.generateProductData();
                    break;
                case 4:
                    $scope.generateTagData();
                    break;
                case 5:
                    $scope.generatePartnerData();
                    break;
            }
            switch($scope.displayMode){
                case 1:
                    $scope.generateGoogleChart10();
                    break;
                case 2:
                    $scope.generateGoogleChart11();
                    break;
                case 3:
                    $scope.generateGoogleChart12();
                    break;
            }
        };
        $scope.performanceComponentByYear = function(){
            switch($scope.limitMode){
                case 1:
                    $scope.generateAreaData();
                    break;
                case 2:
                    $scope.generateAddressData();
                    break;
                case 3:
                    $scope.generateProductData();
                    break;
                case 4:
                    $scope.generateTagData();
                    break;
                case 5:
                    $scope.generatePartnerData();
                    break;
            }
            switch($scope.displayMode){
                case 1:
                    $scope.generateGoogleChart7();
                    break;
                case 2:
                    $scope.generateGoogleChart8();
                    break;
                case 3:
                    $scope.generateGoogleChart9();
                    break;
            }
        };

        $scope.performanceChangeByYear = function(){
            if($scope.year1>$scope.year2){
                swal("年份填写有误");
                return;
            }
            switch($scope.displayMode){
                case 1:
                    $scope.generateData1();
                    $scope.generateGoogleChart1();
                    break;
                case 2:
                    $scope.generateData1();
                    $scope.generateGoogleChart2();
                    break;
                case 3:
                    $scope.generateData3();
                    $scope.generateGoogleChart3();
                    break;
            }

        };
        $scope.performanceChangeBySeason = function(){
            if($scope.year3>$scope.year4){
                swal("年份季度填写有误");
                return;
            }else{
                if($scope.year3==$scope.year4){
                    if($scope.season3>$scope.season4){
                        swal("年份季度填写有误");
                        return;
                    }
                }
                switch($scope.displayMode){
                    case 1:
                        $scope.generateData2();
                        $scope.generateGoogleChart4();
                        break;
                    case 2:
                        $scope.generateData2();
                        $scope.generateGoogleChart5();
                        break;
                    case 3:
                        $scope.generateData4();
                        $scope.generateGoogleChart6();
                }



            }
        };

        $scope.generateGoogleChart11 = function(){
            if($scope.selectedItem2 == null){
                swal("请选择统计范围");
                return;
            }
            if($scope.selectedItem2.friendId==null){
                swal("该成员已不在贵组织");
                return;
            }
            opportunityByCanSee.queryMyTeam({id:$scope.selectedItem2.friendId},function(result){
                _.forEach(result,function(data){
                    if(data.status==1 || data.status==5 || data.status==6){
                        var date = new Date(data.actualDate);
                        var year = date.getFullYear();
                        if( year == $scope.year5){
                            switch($scope.limitMode){
                                case 1:
                                    _.forEach($scope.areaData,function(item){
                                        if(data.region == item.id){
                                            item.amount += data.amount;
                                        }
                                    });
                                    break;
                                case 2:
                                    _.forEach($scope.addressData,function(item){
                                        if(data.province == item.name){
                                            item.amount += data.amount;
                                        }
                                    });
                                    break;
                                case 3:
                                    _.forEach($scope.productData,function(item){
                                        if(data.products.length>0){
                                            var find = false;
                                            _.forEach(data.products,function(item1){
                                                if(item.id == item1.id){
                                                    find = true;
                                                }
                                            });
                                            if(find){
                                                item.amount += data.amount;
                                            }
                                        }
                                    });
                                    break;
                                case 4:
                                    _.forEach($scope.tagData,function(item){
                                        if(data.tags.length>0){
                                            _.forEach(data.tags,function(item1){
                                                if(item1.id == item.id){
                                                    item.amount += data.amount;
                                                }
                                            })
                                        }
                                    });
                                    break;
                                case 5:
                                    _.forEach($scope.partnerData,function(item){
                                        if(data.partnerDTOs.length>0){
                                            var find = false;
                                            _.forEach(data.partnerDTOs,function(item1){
                                                if(item.id == item1.id){
                                                    find = true;
                                                }
                                            });
                                            if(find){
                                                item.amount += data.amount;
                                            }
                                        }
                                    });
                                    break;
                            }
                        }
                    }
                });
                $scope.drawChart11();
            })
        };
        $scope.generateGoogleChart10 = function(){
            if($scope.selectedItem1 == null){
                swal("请选择统计范围");
                return;
            }
            opportunityByEO.query({id:$scope.selectedItem1.friendId},function(result){
                _.forEach(result,function(data){
                    if(data.ownerId == $scope.selectedItem1.friendId){
                        if(data.status==1 || data.status==5 || data.status==6){
                            var date = new Date(data.actualDate);
                            var currMonth = date.getMonth();
                            var year = date.getFullYear();
                            var quart = Math.floor(( currMonth % 3 == 0 ? ( currMonth / 3 ) : ( currMonth / 3 + 1 ) ) );
                            if( year == $scope.year6 && quart == $scope.season6){
                                switch($scope.limitMode){
                                    case 1:
                                        _.forEach($scope.areaData,function(item){
                                            if(data.region == item.id){
                                                item.amount += data.amount;
                                            }
                                        });
                                        break;
                                    case 2:
                                        _.forEach($scope.addressData,function(item){
                                            if(data.province == item.name){
                                                item.amount += data.amount;
                                            }
                                        });
                                        break;
                                    case 3:
                                        _.forEach($scope.productData,function(item){
                                            if(data.products.length>0){
                                                var find = false;
                                                _.forEach(data.products,function(item1){
                                                    if(item.id == item1.id){
                                                        find = true;
                                                    }
                                                });
                                                if(find){
                                                    item.amount += data.amount;
                                                }
                                            }
                                        });
                                        break;
                                    case 4:
                                        _.forEach($scope.tagData,function(item){
                                            if(data.tags.length>0){
                                                _.forEach(data.tags,function(item1){
                                                    if(item1.id == item.id){
                                                        item.amount += data.amount;
                                                    }
                                                })
                                            }
                                        });
                                        break;
                                    case 5:
                                        _.forEach($scope.partnerData,function(item){
                                            if(data.partnerDTOs.length>0){
                                                var find = false;
                                                _.forEach(data.partnerDTOs,function(item1){
                                                    if(item.id == item1.id){
                                                        find = true;
                                                    }
                                                });
                                                if(find){
                                                    item.amount += data.amount;
                                                }
                                            }
                                        });
                                        break;
                                }
                            }
                        }
                    }
                });
                $scope.drawChart10();
            })
        };
        $scope.generateGoogleChart8 = function(){
            if($scope.selectedItem2 == null){
                swal("请选择统计范围");
                return;
            }
            if($scope.selectedItem2.friendId==null){
                swal("该成员已不在贵组织");
                return;
            }
            opportunityByCanSee.queryMyTeam({id:$scope.selectedItem2.friendId},function(result){
                _.forEach(result,function(data){
                    if(data.status==1 || data.status==5 || data.status==6){
                            var date = new Date(data.actualDate);
                            var year = date.getFullYear();
                            if( year == $scope.year5){
                                switch($scope.limitMode){
                                    case 1:
                                        _.forEach($scope.areaData,function(item){
                                            if(data.region == item.id){
                                                item.amount += data.amount;
                                            }
                                        });
                                        break;
                                    case 2:
                                        _.forEach($scope.addressData,function(item){
                                            if(data.province == item.name){
                                                item.amount += data.amount;
                                            }
                                        });
                                        break;
                                    case 3:
                                        _.forEach($scope.productData,function(item){
                                            if(data.products.length>0){
                                                var find = false;
                                                _.forEach(data.products,function(item1){
                                                    if(item.id == item1.id){
                                                        find = true;
                                                    }
                                                });
                                                if(find){
                                                    item.amount += data.amount;
                                                }
                                            }
                                        });
                                        break;
                                    case 4:
                                        _.forEach($scope.tagData,function(item){
                                            if(data.tags.length>0){
                                                _.forEach(data.tags,function(item1){
                                                    if(item1.id == item.id){
                                                        item.amount += data.amount;
                                                    }
                                                })
                                            }
                                        });
                                        break;
                                    case 5:
                                        _.forEach($scope.partnerData,function(item){
                                            if(data.partnerDTOs.length>0){
                                                var find = false;
                                                _.forEach(data.partnerDTOs,function(item1){
                                                    if(item.id == item1.id){
                                                        find = true;
                                                    }
                                                });
                                                if(find){
                                                    item.amount += data.amount;
                                                }
                                            }
                                        });
                                        break;
                                }
                            }
                        }
                });
                $scope.drawChart8();
            })
        };
        $scope.generateGoogleChart7 = function(){
            if($scope.selectedItem1 == null){
                swal("请选择统计范围");
                return;
            }
            opportunityByEO.query({id:$scope.selectedItem1.friendId},function(result){
                _.forEach(result,function(data){
                    if(data.ownerId == $scope.selectedItem1.friendId){
                        if(data.status==1 || data.status==5 || data.status==6){
                            var date = new Date(data.actualDate);
                            var year = date.getFullYear();
                            if( year == $scope.year5){
                                switch($scope.limitMode){
                                    case 1:
                                        _.forEach($scope.areaData,function(item){
                                            if(data.region == item.id){
                                                item.amount += data.amount;
                                            }
                                        });
                                        break;
                                    case 2:
                                        _.forEach($scope.addressData,function(item){
                                            if(data.province == item.name){
                                                item.amount += data.amount;
                                            }
                                        });
                                        break;
                                    case 3:
                                        _.forEach($scope.productData,function(item){
                                            if(data.products.length>0){
                                                var find = false;
                                                _.forEach(data.products,function(item1){
                                                    if(item.id == item1.id){
                                                        find = true;
                                                    }
                                                });
                                                if(find){
                                                    item.amount += data.amount;
                                                }
                                            }
                                        });
                                        break;
                                    case 4:
                                        _.forEach($scope.tagData,function(item){
                                            if(data.tags.length>0){
                                                _.forEach(data.tags,function(item1){
                                                    if(item1.id == item.id){
                                                        item.amount += data.amount;
                                                    }
                                                })
                                            }
                                        });
                                        break;
                                    case 5:
                                        _.forEach($scope.partnerData,function(item){
                                            if(data.partnerDTOs.length>0){
                                                var find = false;
                                                _.forEach(data.partnerDTOs,function(item1){
                                                    if(item.id == item1.orgCompany.id){
                                                        find = true;
                                                    }
                                                });
                                                if(find){
                                                    item.amount += data.amount;
                                                }
                                            }
                                        });
                                        break;
                                }
                            }
                        }
                    }
                });
                $scope.drawChart7();
            })
        };
        $scope.generateGoogleChart6 = function(){
            if($scope.selectedItem3 == null || $scope.selectedItem4 == null){
                swal("请选择统计范围");
                return;
            }
            opportunityByCanSee.queryMyTeam({id:$rootScope.presentAccountInfo.id},function(result){
                _.forEach(result,function(data){
                    if(data.status==1 || data.status==5 || data.status==6){
                        var date = new Date(data.actualDate);
                        var currMonth = date.getMonth();
                        var year = date.getFullYear();
                        var quart = Math.floor(( currMonth % 3 == 0 ? ( currMonth / 3 ) : ( currMonth / 3 + 1 ) ) );
                        if( (year<$scope.year4 && year>$scope.year3) || (year==$scope.year3 && quart>=$scope.season3) || (year==$scope.year4 && quart<=$scope.season4) ){
                            _.forEach($scope.data4,function(item){
                                var year2 = item.season.split("-")[0];
                                var season2 = item.season.split("-")[1];
                                if(year==year2 && quart==season2){
                                    // item.amount+=data.amount;
                                    if(data.ownerId == $scope.selectedItem3.friendId){
                                        item.amount1 += data.amount;
                                    }
                                    if(data.ownerId == $scope.selectedItem4.friendId){
                                        item.amount2 += data.amount;
                                    }
                                }
                            })
                        }
                    }
                });
                // console.log($scope.data4);
                $scope.drawChart6();
            })
        };
        $scope.generateGoogleChart5 = function(){
            if($scope.selectedItem2 == null){
                swal("请选择统计范围");
                return;
            }
            if($scope.selectedItem2.friendId==null){
                swal("该成员已不在贵组织");
                return;
            }
            opportunityByCanSee.queryMyTeam({id:$scope.selectedItem2.friendId},function(result){
                _.forEach(result,function(data){
                    if(data.status==1 || data.status==5 || data.status==6){
                        var date = new Date(data.actualDate);
                        var currMonth = date.getMonth();
                        var year = date.getFullYear();
                        var quart = Math.floor(( currMonth % 3 == 0 ? ( currMonth / 3 ) : ( currMonth / 3 + 1 ) ) );
                        if( (year<$scope.year4 && year>$scope.year3) || (year==$scope.year3 && quart>=$scope.season3) || (year==$scope.year4 && quart<=$scope.season4) ){
                            _.forEach($scope.data2,function(item){
                                var year2 = item.season.split("-")[0];
                                var season2 = item.season.split("-")[1];
                                if(year==year2 && quart==season2){
                                    item.amount+=data.amount;
                                }
                            })
                        }
                    }
                });
                $scope.drawChart5();
            })
        };
        $scope.generateGoogleChart4 = function(){
            if($scope.selectedItem1 == null){
                swal("请选择统计范围");
                return;
            }
            opportunityByEO.query({id:$scope.selectedItem1.friendId},function(result){
                _.forEach(result,function(data){
                    if(data.ownerId == $scope.selectedItem1.friendId){
                        if(data.status==1 || data.status==5 || data.status==6){
                            var date = new Date(data.actualDate);
                            var currMonth = date.getMonth();
                            var year = date.getFullYear();
                            var quart = Math.floor(( currMonth % 3 == 0 ? ( currMonth / 3 ) : ( currMonth / 3 + 1 ) ) );
                            if( (year<$scope.year4 && year>$scope.year3) || (year==$scope.year3 && quart>=$scope.season3) || (year==$scope.year4 && quart<=$scope.season4) ){
                                _.forEach($scope.data2,function(item){
                                    var year2 = item.season.split("-")[0];
                                    var season2 = item.season.split("-")[1];
                                    if(year==year2 && quart==season2){
                                        item.amount+=data.amount;
                                    }
                                })
                            }
                        }
                    }
                });
                $scope.drawChart4();
            })
        };
        $scope.generateGoogleChart3 = function(){
            if($scope.selectedItem3 == null || $scope.selectedItem4 == null){
                swal("请选择统计范围");
                return;
            }
            opportunityByCanSee.queryMyTeam({id:$rootScope.presentAccountInfo.id},function(result){
                _.forEach(result,function(data){
                    if(data.status==1 || data.status==5 || data.status==6){
                        var date = new Date(data.actualDate);
                        if(date.getFullYear()>=$scope.year1 && date.getFullYear()<=$scope.year2){
                            _.forEach($scope.data3,function(item){
                                if(date.getFullYear()==item.year){
                                    // item.amount+=data.amount;
                                    if(data.ownerId == $scope.selectedItem3.friendId){
                                        item.amount1 += data.amount;
                                    }
                                    if(data.ownerId == $scope.selectedItem4.friendId){
                                        item.amount2 += data.amount;
                                    }
                                }
                            });
                        }
                    }
                });
                // console.log($scope.data3);
                $scope.drawChart3();
            })
        };
        $scope.generateGoogleChart2 = function(){
            if($scope.selectedItem2 == null){
                swal("请选择统计范围");
                return;
            }
            if($scope.selectedItem2.friendId==null){
                swal("该成员已不在贵组织");
                return;
            }
            opportunityByCanSee.queryMyTeam({id:$scope.selectedItem2.friendId},function(result){
                _.forEach(result,function(data){
                    if(data.status==1 || data.status==5 || data.status==6){
                        var date = new Date(data.actualDate);
                        if(date.getFullYear()>=$scope.year1 && date.getFullYear()<=$scope.year2){
                            _.forEach($scope.data1,function(item){
                                if(date.getFullYear()==item.year){
                                    item.amount+=data.amount;
                                }
                            });
                        }
                    }
                });
                $scope.drawChart2();
            })
        };
        $scope.generateGoogleChart1 = function(){
            if($scope.selectedItem1==null){
                swal("请选择统计范围");
                return;
            }
            opportunityByEO.query({id:$scope.selectedItem1.friendId},function(result){
                _.forEach(result,function(data){
                    // 1 win 5 close 6 done
                    // console.log(data.status);
                    // console.log(data);
                    if(data.ownerId == $scope.selectedItem1.friendId){
                        if(data.status==1 || data.status==5 || data.status==6){
                            var date = new Date(data.actualDate);
                            if(date.getFullYear()>=$scope.year1 && date.getFullYear()<=$scope.year2){
                                _.forEach($scope.data1,function(item){
                                    if(date.getFullYear()==item.year){
                                        item.amount+=data.amount;
                                    }
                                });
                            }
                        }
                    }
                });
                $scope.drawChart1();
            })
        };

        $scope.generatePartnerData = function(){
            $scope.partnerData = [];
            _.forEach($scope.partnerList,function(data){
                $scope.partnerData.push({
                    name:data.name,
                    id:data.id,
                    amount:0
                })
            });
        };
        $scope.generateTagData = function(){
            $scope.tagData = [];
            _.forEach($scope.tagList,function(data){
                $scope.tagData.push({
                    name:data.name,
                    id:data.id,
                    amount:0
                })
            })
        };
        $scope.generateProductData = function(){
            $scope.productData = [];
            _.forEach($scope.productList,function(data){
                $scope.productData.push({
                    name:data.name,
                    id:data.id,
                    amount:0
                })
            });
        };
        $scope.generateAddressData = function(){
            $scope.addressData = [];
            _.forEach($scope.provinceList,function(data){
                $scope.addressData.push({
                    name:data,
                    amount:0
                })
            })
        };
        $scope.generateAreaData = function(){
            $scope.areaData = [];
            _.forEach($scope.areaList,function(data){
                $scope.areaData.push({
                    name:data.name,
                    id:data.id,
                    amount:0
                });
            });
        };
        $scope.generateData4 = function(){
            $scope.data4 = [];
            for(var i=$scope.year3;i<=$scope.year4;i++){
                if(i==$scope.year3){
                    for(var j=$scope.season3;j<=4;j++){
                        $scope.data4.push({
                            season:i+"-"+j,
                            amount1:0,
                            amount2:0
                        })
                    }
                } else if(i==$scope.year4){
                    for(var j=1;j<=$scope.season4;j++){
                        $scope.data4.push({
                            season:i+"-"+j,
                            amount1:0,
                            amount2:0
                        })
                    }
                } else{
                    for(var j=1;j<=4;j++){
                        $scope.data4.push({
                            season:i+"-"+j,
                            amount1:0,
                            amount2:0
                        })
                    }
                }
            }
        };
        $scope.generateData2 = function(){
            $scope.data2 = [];
            for(var i=$scope.year3;i<=$scope.year4;i++){
                if(i==$scope.year3){
                    for(var j=$scope.season3;j<=4;j++){
                        $scope.data2.push({
                            season:i+"-"+j,
                            amount:0
                        })
                    }
                } else if(i==$scope.year4){
                    for(var j=1;j<=$scope.season4;j++){
                        $scope.data2.push({
                            season:i+"-"+j,
                            amount:0
                        })
                    }
                } else{
                    for(var j=1;j<=4;j++){
                        $scope.data2.push({
                            season:i+"-"+j,
                            amount:0
                        })
                    }
                }
            }
        };
        $scope.generateData3 = function(){
            $scope.data3 = [];
            for(var i=$scope.year1;i<=$scope.year2;i++){
                $scope.data3.push({
                    year:i,
                    amount1:0,
                    amount2:0
                })
            }
        };
        $scope.generateData1 = function(){
            $scope.data1 = [];
            for(var i=$scope.year1;i<=$scope.year2;i++){
                $scope.data1.push({
                    year:i,
                    amount:0
                });
            }
        };

        $scope.resetAreaData = function(){
            for(var i=0;i<$scope.areaData.length;i++){
                if($scope.areaData[i].amount==0){
                    $scope.areaData.splice(i,1);
                    i--;
                }
            }
        };
        $scope.resetAddressData = function(){
            for(var i=0;i<$scope.addressData.length;i++){
                if($scope.addressData[i].amount==0){
                    $scope.addressData.splice(i,1);
                    i--;
                }
            }
        };
        $scope.resetProductData = function(){
            for(var i=0;i<$scope.productData.length;i++){
                if($scope.productData[i].amount==0){
                    $scope.productData.splice(i,1);
                    i--;
                }
            }
        };
        $scope.resetTagData = function(){
            for(var i=0;i<$scope.tagData.length;i++){
                if($scope.tagData[i].amount==0){
                    $scope.tagData.splice(i,1);
                    i--;
                }
            }
        };
        $scope.resetPartnerData = function(){
            for(var i=0;i<$scope.partnerData.length;i++){
                if($scope.partnerData[i].amount==0){
                    $scope.partnerData.splice(i,1);
                    i--;
                }
            }
        };

        $scope.drawChart11 = function(){
            var allItem = [["分类","所占份额"]];
            switch($scope.limitMode){
                case 1:
                    $scope.resetAreaData();
                    $scope.componentData = $scope.areaData;
                    break;
                case 2:
                    $scope.resetAddressData();
                    $scope.componentData = $scope.addressData;
                    break;
                case 3:
                    $scope.resetProductData();
                    $scope.componentData = $scope.productData;
                    break;
                case 4:
                    $scope.resetTagData();
                    $scope.componentData = $scope.tagData;
                    break;
                case 5:
                    $scope.resetPartnerData();
                    $scope.componentData = $scope.partnerData;
                    break;
            }
            _.forEach($scope.componentData,function(data){
                var item = [];
                item.push(data.name);
                item.push(data.amount);
                allItem.push(item);
            });
            console.log(allItem);
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:$scope.selectedItem2.baseInfo.name + "团队季度业绩成分分析",
                chartArea:{
                    left:"300"
                }
            };
            var chart = new google.visualization.PieChart(document.getElementById('chart_pie_1'));
            chart.draw(data,options);
        };
        $scope.drawChart10 = function(){
            var allItem = [["分类","所占份额"]];
            switch($scope.limitMode){
                case 1:
                    $scope.resetAreaData();
                    $scope.componentData = $scope.areaData;
                    break;
                case 2:
                    $scope.resetAddressData();
                    $scope.componentData = $scope.addressData;
                    break;
                case 3:
                    $scope.resetProductData();
                    $scope.componentData = $scope.productData;
                    break;
                case 4:
                    $scope.resetTagData();
                    $scope.componentData = $scope.tagData;
                    break;
                case 5:
                    $scope.resetPartnerData();
                    $scope.componentData = $scope.partnerData;
                    break;
            }
            _.forEach($scope.componentData,function(data){
                var item = [];
                item.push(data.name);
                item.push(data.amount);
                allItem.push(item);
            });
            console.log(allItem);
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:$scope.selectedItem2.baseInfo.name + "团队季度业绩成分分析",
                chartArea:{
                    left:"300"
                }
            };
            var chart = new google.visualization.PieChart(document.getElementById('chart_pie_1'));
            chart.draw(data,options);
        };
        $scope.drawChart8 = function(){
            var allItem = [["分类","所占份额"]];
            switch($scope.limitMode){
                case 1:
                    $scope.resetAreaData();
                    $scope.componentData = $scope.areaData;
                    break;
                case 2:
                    $scope.resetAddressData();
                    $scope.componentData = $scope.addressData;
                    break;
                case 3:
                    $scope.resetProductData();
                    $scope.componentData = $scope.productData;
                    break;
                case 4:
                    $scope.resetTagData();
                    $scope.componentData = $scope.tagData;
                    break;
                case 5:
                    $scope.resetPartnerData();
                    $scope.componentData = $scope.partnerData;
                    break;
            }
            _.forEach($scope.componentData,function(data){
                var item = [];
                item.push(data.name);
                item.push(data.amount);
                allItem.push(item);
            });
            console.log(allItem);
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:$scope.selectedItem2.baseInfo.name + "团队年度业绩成分分析",
                chartArea:{
                    left:"300"
                }
            };
            var chart = new google.visualization.PieChart(document.getElementById('chart_pie_1'));
            chart.draw(data,options);
        };
        $scope.drawChart7 = function(){
            var allItem = [["分类","所占份额"]];
            switch($scope.limitMode){
                case 1:
                    $scope.resetAreaData();
                    $scope.componentData = $scope.areaData;
                    break;
                case 2:
                    $scope.resetAddressData();
                    $scope.componentData = $scope.addressData;
                    break;
                case 3:
                    $scope.resetProductData();
                    $scope.componentData = $scope.productData;
                    break;
                case 4:
                    $scope.resetTagData();
                    $scope.componentData = $scope.tagData;
                    break;
                case 5:
                    $scope.resetPartnerData();
                    $scope.componentData = $scope.partnerData;
                    break;
            }
            _.forEach($scope.componentData,function(data){
                var item = [];
                item.push(data.name);
                item.push(data.amount);
                allItem.push(item);
            });
            console.log(allItem);
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:$scope.selectedItem1.baseInfo.name + "个人年度业绩成分分析",
                chartArea:{
                    left:"300"
                }
            };
            var chart = new google.visualization.PieChart(document.getElementById('chart_pie_1'));
            chart.draw(data,options);

        };
        $scope.drawChart6 = function(){
            var allItem = [["季度",$scope.selectedItem3.baseInfo.name,$scope.selectedItem4.baseInfo.name]];
            _.forEach($scope.data4,function(data){
                var item=[];
                item.push(data.season);
                item.push(data.amount1);
                item.push(data.amount2);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:$scope.selectedItem3.baseInfo.name+"VS"+ $scope.selectedItem4.baseInfo.name +" 季度业绩变化 单位：万元",
                hAxis:{
                    // baseline:2
                    gridlines:{  //图表竖线的控制
                        count:$scope.data4.length,
                        scaleType:"log",
                        title:"年份"
                    }
                }
                // legend:{position:"bottom"}
            };
            var chart = new google.visualization.LineChart(document.getElementById('chart_line_1'));
            chart.draw(data,options);
        };
        $scope.drawChart5 = function(){
            var allItem = [["季度","销售额"]];
            _.forEach($scope.data2,function(data){
                var item=[];
                item.push(data.season);
                item.push(data.amount);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:$scope.selectedItem2.baseInfo.name+"团队季度业绩变化 单位：万元",
                hAxis:{
                    // baseline:2
                    gridlines:{  //图表竖线的控制
                        count:$scope.data2.length,
                        scaleType:"log",
                        title:"季度"
                    }
                }
                // legend:{position:"bottom"}
            };
            var chart = new google.visualization.LineChart(document.getElementById('chart_line_1'));
            chart.draw(data,options);
        };
        $scope.drawChart4 = function(){
            var allItem = [["季度","销售额"]];
            _.forEach($scope.data2,function(data){
                var item=[];
                item.push(data.season);
                item.push(data.amount);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:$scope.selectedItem1.baseInfo.name+"个人季度业绩变化 单位：万元",
                hAxis:{
                    // baseline:2
                    gridlines:{  //图表竖线的控制
                        count:$scope.data2.length,
                        scaleType:"log",
                        title:"季度"
                    }
                }
                // legend:{position:"bottom"}
            };
            var chart = new google.visualization.LineChart(document.getElementById('chart_line_1'));
            chart.draw(data,options);
        };
        $scope.drawChart3 = function(){
            var allItem = [["年份",$scope.selectedItem3.baseInfo.name,$scope.selectedItem4.baseInfo.name]];
            _.forEach($scope.data3,function(data){
                var item=[];
                item.push(data.year);
                item.push(data.amount1);
                item.push(data.amount2);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:$scope.selectedItem3.baseInfo.name+"VS"+ $scope.selectedItem4.baseInfo.name +" 年度业绩变化 单位：万元",
                hAxis:{
                    // baseline:2
                    gridlines:{  //图表竖线的控制
                        count:$scope.data3.length,
                        scaleType:"log",
                        title:"年份"
                    }
                }
                // legend:{position:"bottom"}
            };
            var chart = new google.visualization.LineChart(document.getElementById('chart_line_1'));
            chart.draw(data,options);
        };
        $scope.drawChart2 = function(){
            var allItem = [["年份","销售额"]];
            _.forEach($scope.data1,function(data){
                var item=[];
                item.push(data.year);
                item.push(data.amount);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);
            var options = {
                title:$scope.selectedItem1.baseInfo.name+"团队年度业绩变化 单位：万元",
                hAxis:{
                    // baseline:2
                    gridlines:{  //图表竖线的控制
                        count:$scope.data1.length,
                        scaleType:"log",
                        title:"年份"
                    }
                }
                // legend:{position:"bottom"}
            };
            var chart = new google.visualization.LineChart(document.getElementById('chart_line_1'));
            chart.draw(data,options);
        };
        $scope.drawChart1 = function(){
            var allItem = [["年份","销售额"]];
            _.forEach($scope.data1,function(data){
                var item=[];
                item.push(data.year);
                item.push(data.amount);
                allItem.push(item);
            });
            var data = google.visualization.arrayToDataTable(allItem);

            var options = {
                title:$scope.selectedItem1.baseInfo.name+"个人年度业绩变化 单位：万元",
                hAxis:{
                    // baseline:2
                    gridlines:{  //图表竖线的控制
                        count:$scope.data1.length,
                        scaleType:"log",
                        title:"年份"
                    }
                }
                // legend:{position:"bottom"}
            };
            var chart = new google.visualization.LineChart(document.getElementById('chart_line_1'));
            chart.draw(data,options);
        };



























        $scope.provinceList = [
            "北京","天津","河北","山西","内蒙古","辽宁","吉林","黑龙江","上海","江苏",
            "浙江","安徽","福建","江西","山东","河南","湖北","湖南","广东","广西",
            "海南","重庆","四川","贵州","云南","西藏","陕西","甘肃","青海","宁夏","新疆"
        ];




        $scope.companyList = null;
        $scope.getOrgCompanyList = function(){
            OrgCompanyInfo.queryByEO({id:$rootScope.presentAccountInfo.id},function(result){
                $scope.companyList = result;
                _.forEach(result,function(data){
                    $scope.partnerList.push({
                        name:data.companyBaseInfo.name,
                        id:data.id,
                        select:false
                    })
                });
                console.log($scope.companyList);
            });
        };
        $scope.getOrgCompanyList();

        $scope.getAllTagSet=function(){
            $scope.allTagSet=[];
            tagSetByOrg.query({id:$rootScope.currentOrganization.id},function(result){
                $scope.allTagSet=result;
                $scope.tagList=[];
                angular.forEach($scope.allTagSet,function (item) {
                    angular.forEach(item.tagList,function (x) {
                        x["select"] = false;
                        $scope.tagList.push(x);
                    })
                });
                // console.log($scope.allTagSet);
            });
        };
        $scope.getAllTagSet();

        $scope.getAllProduct = function(){
            productNew.query({},function(result){
                _.forEach(result,function(item){
                    if(item.type == 3){
                        $scope.productList.push({
                            name:item.name,
                            id:item.id,
                            select:false
                        })
                    }
                });
            })
        };
        $scope.getAllProduct();


        $scope.filterList= ["客户","地域","地点","产品线","标签","合作伙伴"];
        $scope.areaList=[
            {
                name:'华北',
                id:1,
                select:false
            },
            {
                name:'东北',
                id:2,
                select:false
            },
            {
                name:'华南',
                id:3,
                select:false
            },
            {
                name:'西北',
                id:4,
                select:false
            },
            {
                name:'西部',
                id:5,
                select:false
            },
            {
                name:'中部',
                id:6,
                select:false
            },
            {
                name:'华东',
                id:7,
                select:false
            }
        ];
        $scope.changeLimitation=function (num) {
            $scope.filterName=$scope.filterList[num];
            if($scope.choseAddress.city==""){
                $scope.choseAddress.city=null;
            }
            if($scope.choseAddress.district==""){
                $scope.choseAddress.district=null;
            }
            if($scope.choseAddress.street==""){
                $scope.choseAddress.street=null;
            }


            ngDialog.open({
                templateUrl:'scripts/components/organizationBusiness/oppPerformance/selectLimit.html',
                scope:$scope,
                controller:["$scope",function(){
                    $scope.type=num;
                    $scope.closeDialog = function(){
                        ngDialog.close();
                    };
                }]
            })
        };

        $scope.$watch(function(){
            return $scope.choseAddress.province;
        },function(){
            if($scope.choseAddress.province==null || $scope.choseAddress.province==""){
                $scope.initChooseAddess();
                $scope.address = false;
            }else{
                $scope.address = true;
            }
        });

        $scope.chooseArea=function (area) {
            var tag=false;
            _.forEach($scope.areaList,function (item) {
                if(item.id==area.id){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }else{
                    var find = false;
                    _.forEach($scope.areaList,function(data){
                        if(data.select){
                            find = true;
                        }
                    });
                    if(!find){
                        tag = false;
                    }
                }
            });
            $scope.area=tag;
        };
        $scope.chooseProduct=function (product) {
            var tag=false;
            _.forEach($scope.productList,function (item) {
                if(item.id==product.id){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }else{
                    var find = false;
                    _.forEach($scope.productList,function(data){
                        if(data.select){
                            find = true;
                        }
                    });
                    if(!find){
                        tag = false;
                    }
                }
            });
            $scope.product=tag;
        };
        $scope.choosePartner=function (custom) {
            var tag=false;
            _.forEach($scope.partnerList,function (item) {
                if(item.id==custom.id){
                    item.select=!item.select;
                }
                if (item.select){
                    tag=true;
                }else{
                    var find = false;
                    _.forEach($scope.partnerList,function(data){
                        if(data.select){
                            find = true;
                        }
                    });
                    if(!find){
                        tag = false;
                    }
                }
            });
            $scope.partner=tag;
        };
        $scope.chooseTag = function(tagInfo){
            var tag = false;
            _.forEach($scope.allTagSet,function(item){
                _.forEach(item.tagList,function(data){
                    if(data.id == tagInfo.id){
                        data.select = !data.select;
                    }
                    if(data.select){
                        tag = true;
                    }else{
                        var find = false;
                        _.forEach($scope.allTagSet,function(item1){
                            _.forEach(item1.tagList,function(item2){
                                if(item2.select){
                                    find = true;
                                }
                            })
                        });
                        if(!find){
                           tag = false;
                        }
                    }
                })
            });
            $scope.tag = tag;


        }





    });